package com.jrx.anytxn.param.controller.partner;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.partner.PartnerShareProfitReq;
import com.jrx.anytxn.param.dto.partner.PartnerShareProfitRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.partner.IPartnerShareProfitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "合作方分润配置API接口")
@RestController
@RequestMapping(value = "/param/partner/shareProfit")
public class PartnerShareProfitController {

    private static final Logger logger = LoggerFactory.getLogger(PartnerShareProfitController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private IPartnerShareProfitService partnerShareProfitService;

    @ApiOperation(value = "根据租户ID和商户ID查询", notes = "根据租户ID和商户ID查询")
    @GetMapping(value = "/{tenantId}/{shareProfitId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "shareProfitId", value = "分润参数ID", required = true)
    })
    public TxnRespResult<PartnerShareProfitRes> getById(@PathVariable String tenantId,
                                                        @PathVariable String shareProfitId) {
        TxnRespResult<PartnerShareProfitRes> result = new TxnRespResult<>();
        PartnerShareProfitRes res;
        try {
            res = partnerShareProfitService.getByTenIdIdAndShareProfitId(tenantId, shareProfitId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增分润参数", notes = "新增分润参数")
    @PostMapping("/")
    public TxnRespResult<PartnerShareProfitRes> add(@RequestBody PartnerShareProfitReq partnerShareProfitReq) {
        TxnRespResult<PartnerShareProfitRes> result = new TxnRespResult<>();
        PartnerShareProfitRes res;
        try{
            partnerShareProfitReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            partnerShareProfitReq.setUpdateBy(infoService.getUser().getUserName());
            Date lastShareDate = DateUtils.getDate(partnerShareProfitReq.getLastShareDate(),1);
            Date nextShareDate = DateUtils.getDate(partnerShareProfitReq.getNextShareDate(),1);
            partnerShareProfitReq.setLastShareDate(lastShareDate);
            partnerShareProfitReq.setNextShareDate(nextShareDate);
            res = partnerShareProfitService.addPrShareProfitTable(partnerShareProfitReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询分润参数", notes = "分页查询分润参数")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PartnerShareProfitRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PartnerShareProfitRes>> result = new TxnRespResult<>();
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            TxnPage<PartnerShareProfitRes>  res = partnerShareProfitService.findAllPageByTenantId(pageNum, pageSize, tenantId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "更新分润参数信息", notes = "更新分润参数信息")
    @PutMapping("/")
    public TxnRespResult<PartnerShareProfitRes> modifyPmPartnerInfo(@RequestBody PartnerShareProfitReq req) {
        TxnRespResult<PartnerShareProfitRes> result = new TxnRespResult<>();
        PartnerShareProfitRes res;
        try {
            req.setUpdateBy(infoService.getUser().getUserName());
            Date lastShareDate = DateUtils.getDate(req.getLastShareDate(),1);
            Date nextShareDate = DateUtils.getDate(req.getNextShareDate(),1);
            req.setLastShareDate(lastShareDate);
            req.setNextShareDate(nextShareDate);

            res = partnerShareProfitService.modifyPartnerShareProfitTable(req);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和分润参数ID删除信息", notes = "根据租户ID和分润参数ID删除信息")
    @DeleteMapping(value = "/{tenantId}/{shareProfitId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "shareProfitId", value = "分润参数ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndPartnerId(@PathVariable String tenantId, @PathVariable String shareProfitId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            Boolean flag =
                    partnerShareProfitService.removeByTenantIdAndShareProfitId(tenantId, shareProfitId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "根据机构号和租户ID查询分润参数", notes = "根据机构号和租户ID查询分润参数")
    @GetMapping(value = "/organizationId/{organizationId}")
    @ApiImplicitParam(name = "organizationId", value = "机构号", required = true)
    public TxnRespResult<ArrayList<PartnerShareProfitRes>> getShareProfitByOrgId(@PathVariable String organizationId) {
        TxnRespResult<ArrayList<PartnerShareProfitRes>> result = new TxnRespResult<>();
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        List<PartnerShareProfitRes> list = partnerShareProfitService.getPartnerShareProfitByOrgIdAndTenId(organizationId, tenantId);
        return result.getSuccess((ArrayList) list);
    }
}
